<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/9/19
 * Time: 11:15
 */

use yii\helpers\Url;

$web = Url::base();
?>


<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>

<script type="application/javascript">
    var to_institution;
    $(function () {
        <?php
        if($item['is_enabled'] == true){
        ?>
        $("#is_enabled").bootstrapSwitch('state', true);
        <?php
        }else{
        ?>
        $("#is_enabled").bootstrapSwitch('state', false);
        <?php
        }
        ?>
        var to_institution_data =  <?= json_encode($to_institutions) ?>;
        to_institution = $('#to_institution').treeAutocomplete({
            defaultText: '机构',
            source: to_institution_data,
            onSelected: function (element) {
                var data = {};
                data.institution_code = element.getValue();
                loading.show();
                requestHtml('bargain-part-view', data, true, function (responseHtml) {
                    $("#modaledit").html(responseHtml);
                    loading.hide();
                });
            }
        });


        <?php
        if($institution_code != ''){
        ?>
        to_institution.setValue('<?=$institution_code?>');
        <?php
        }
        ?>

        <?php
        if($item != null){
        ?>
        to_institution.setDisabled(true);
        <?php
        }
        ?>

//        //初始化checkbox样式
//        $('.square-blue input').iCheck({
//            checkboxClass: 'icheckbox_square-blue',
//            radioClass: 'iradio_square-blue',
//            increaseArea: '20%' // optional
//        });
    });

    function checkBargain(obj) {
        var oldPriceTextBox = $(obj).parent().parent().find('[name="old_price"]').find("input");
//        var priceTextBox = $(obj).parent().parent().find('[name="price"]').find("input");
        if ($(obj).is(':checked')) {
            oldPriceTextBox.removeAttr("disabled");
//            priceTextBox.removeAttr("disabled");
        } else {
            oldPriceTextBox.attr("disabled", "disabled");
//            priceTextBox.attr("disabled", "disabled");
        }
    }

    var times = 0;
    function checkDate() {
        var c = $("#is_enabled").is(':checked');
        if (!c) {
            return false;
        }

        if (times != undefined) {
            var start_datetime = $("#modaledit_submit_form").find("#start_datetime").val();
            var end_datetime = $("#modaledit_submit_form").find("#end_datetime").val();
            if (!compareDate(start_datetime, end_datetime)) {
                alert('结束时间必须大于开始时间');
                $("#is_enabled").bootstrapSwitch('state', false);
                return false;
            }
            var now = timestampToDatetime(new Date());
            if (!compareDate(now, end_datetime)) {
                alert('结束时间必须大于当前时间');
                $("#is_enabled").bootstrapSwitch('state', false);
                return false;
            }
        }
    }
</script>

<div class="form-horizontal" id="modaledit_submit_form">
    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">机构名称：</label>
        <div class="col-lg-10">
            <div id="to_institution"></div>
        </div>
    </div>

    <div class="form-group" style=" margin-top: 5px">
        <input type="hidden" id="id" name="id" class="form-control"
               value="<?= $item != null ? $item['id'] : '' ?>">
        <label class="control-label col-lg-2" for="exampleInputPassword2">活动名称：</label>
        <div class="col-lg-10">
            <input type="text" id="name" name="name" class="form-control"
                   value="<?= $item != null ? $item['name'] : '' ?>">
        </div>
    </div>

    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" class="control-label" for="exampleInputPassword2">活动描述：</label>
        <div class="col-lg-10">
                <textarea id="description" name="description" class="form-control"
                          rows="3"><?= $item != null ? $item['description'] : '' ?></textarea>
        </div>
    </div>

    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="dtp_start_datetime">开始时间：</label>
        <div class="col-lg-4">
            <div data-date="" data-link-field="dtp_start_datetime" class="input-group date form_datetime-adv">
                <input type="text" class="form-control" readonly="" size="16" id="start_datetime"
                       name="start_datetime"
                       value="<?= $item != null ? $item['start_datetime'] : '' ?>">
                <div class="input-group-btn">
                    <button type="button" class="btn btn-primary date-reset"><i class="fa fa-times"></i>
                    </button>
                    <button type="button" class="btn btn-success date-set"><i class="fa fa-calendar"></i>
                    </button>
                </div>
            </div>
        </div>
        <label class="control-label col-lg-2" for="exampleInputPassword2">结束时间：</label>
        <div class="col-lg-4">
            <div data-date="" class="input-group date form_datetime-adv">
                <input type="text" class="form-control" readonly="" size="16" id="end_datetime"
                       name="end_datetime"
                       value="<?= $item != null ? $item['end_datetime'] : '' ?>">
                <div class="input-group-btn">
                    <button type="button" class="btn btn-primary date-reset"><i class="fa fa-times"></i>
                    </button>
                    <button type="button" class="btn btn-success date-set"><i class="fa fa-calendar"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="form-group" style=" margin-top: 5px">
        <label class="control-label col-lg-2" for="exampleInputPassword2">是否启用：</label>
        <div class="col-lg-4">
            <div class="switch">
                <input type="checkbox" id="is_enabled" name="is_enabled" onchange="checkDate()"
                    <?php if ($item != null && $item['is_enabled'] == true) {
                        echo 'checked="checked"';
                    } ?>
                ><span
                    class="btn-clipboard popovers" data-trigger="hover"
                    data-placement="right"
                    data-content=""
                    data-original-title="提示"></span>
            </div>
        </div>
    </div>

    <div class="form-group" style=" margin-top: 5px;">
        <div class="col-lg-12">
            <table class="table table-bordered table-hover table-condensed" id="goods_table">
                <thead class="nowrap">
                <tr>
                    <th class="col-lg-1"></th>
                    <th class="col-lg-2">产品编码</th>
                    <th class="col-lg-6">产品名</th>
                    <th class="col-lg-2">原价</th>
                    <th class="col-lg-2">特价</th>
                    <th class="col-lg-2">首页显示</th>
                </tr>
                </thead>
                <tbody>
                <?php
                if ($goods != null && $goods != '') {
                    for ($i = 0; $i < count($goods); $i++) {
                        ?>
                        <tr>
                            <td>
                                <input onclick="checkBargain(this)" type="checkbox"
                                       name="goods" <?= $goods[$i]['activity_id'] != null ? 'checked="checked"' : '' ?>
                                       value="<?= $goods[$i]['code'] ?>">
                            </td>
                            <td name="code"><?= $goods[$i]['code'] ?></td>
                            <td name="name"><?= $goods[$i]['name'] ?></td>
                            <td name="old_price">
                                <input type="number" <?= $goods[$i]['activity_id'] == null ? 'disabled' : '' ?>
                                       value="<?= $goods[$i]['old_price'] ?>">
                            </td>
                            <td name="price"><?= $goods[$i]['price'] ?></td>
                            <td name="is_top">
                                <input
                                    type="checkbox" <?= $goods[$i]['is_top'] != null && $goods[$i]['is_top'] != 0 ? 'checked="checked"' : '' ?>>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
